﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SurfaceController : MonoBehaviour, IEventListener
{
    public SurfaceData Data;

    private bool is_Spawn;

    private void Awake()
    {
        EventNodeCore.GetInstance().AttachEventListener(EventDef.CubeRotate, this);
        is_Spawn = true;
        //Initiate();
    }
    private void OnEnable()
    {
        Initiate();
    }
    private void OnDestroy()
    {
        EventNodeCore.GetInstance().DetachEventListener(EventDef.CubeRotate, this);
    }
    /// <summary>
    /// 初始化方法
    /// </summary>
    public void Initiate()
    {
        FlushCube();
        MapData.Instance.Registe(Data);
    }
    private void OnDisable()
    {
        MapData.Instance.Unregiste(Data);
    }
    /// <summary>
    /// 刷新Surface数据
    /// </summary>
    private void FlushCube()
    {
        if (Data == null)
        {
            Data = new SurfaceData() { Obj = gameObject };
        }
        if ((Data.Index - transform.position + transform.parent.position).magnitude > 0.01)
        {
            Data.Index = new Vector3Int((int)(transform.position.x - transform.parent.position.x), (int)(transform.position.y - transform.parent.position.y), (int)(transform.position.z - transform.parent.position.z));
        }
        Data.Cube = transform.parent.GetComponent<CubeController>().Data;
        if (Data.Controller != this) Data.Controller = this;
        if (Data.Dices == null) Data.Dices = new Dictionary<Vector3, DiceData>();
        name = string.Concat("Surface" + Data.Index.ToString());
    }
    public bool HandleEvent(int id, object param1, object param2)
    {
        /////收到来自Cube的CubeRotate信号，Suface对其子物体发出旋转翻面命令
        //if (id == EventDef.CubeRotate)
        //{
        //    //先判断子物体是否有Player，如果有，这个面就不执行生成新物体的操作
        //    foreach (Transform child in transform)
        //    {
        //        if (child.GetComponent<DiceController>().Data.ObjectOfGround != null)
        //            if (child.GetComponent<DiceController>().Data.ObjectOfGround.tag == "Player")
        //            {
        //                is_Spawn = false;
        //            }
        //    }
        //    //如果没有Player就生成新的物体
        //    if (is_Spawn)
        //    {

        //        //上面注释的是来实现某一个面不翻转，现在这个算法是单独不让Player那一个Dice不翻转和生成新物体
        //        //CSV的行和列
        //        int row = Random.Range(1, 4);
        //        if (param1 != null)
        //            if (param1.GetType().ToString().Equals("System.Int32"))
        //            {
        //                row = (int)param1;
        //            }
        //        int column = 0;
        //        foreach (Transform child in transform)
        //        {
        //            if (child.GetComponent<DiceController>().Data.ObjectOfGround != null)
        //            {
        //                if (child.GetComponent<DiceController>().Data.ObjectOfGround.tag == "Player")
        //                {
        //                    break;
        //                }
        //            }
        //            string prefabname = TableAgent.Instance.GetString("Suface", row.ToString(), column.ToString());
        //            column = column + 1;
        //            MapManager.Instance.SpawnObj(child.GetComponent<DiceController>().Data, prefabname);
        //        }
        //    }
        //    is_Spawn = true;
        //}
        return false;
    }
    /// <summary>
    /// 判断此surface能否翻转，可以就返回true
    /// </summary>
    /// <returns></returns>
    public bool CanFlip()
    {
        foreach (var item in Data.Dices)
        {
            if (!item.Value.Controller.CanFlip())
                return false;
        }
        return true;
    }

    public bool IsPlayerHere()
    {
        if (PlayerManager.Instance.Data.CoorDinate.Surface == Data)
        {
            return true;
        }
        return false;
    }


    public int EventPriority()
    {
        return 0;
    }
    /// <summary>
    /// 生成新的面上的物体 
    /// </summary>
    public void SpawnObj(string row = null)
    {
        if (PlayerManager.Instance.Data.CoorDinate.Surface != Data)
        {
            int column = 0;
            if (row != null)
            {
                foreach (var dice in Data.Dices)
                {
                    string prefabname = TableAgent.Instance.GetString("Suface", row, column.ToString());
                    column = column + 1;
                    MapManager.Instance.SpawnObj(dice.Value, prefabname);
                }
            }
            else
            {
                int rows = GameManager.Instance.Row;
                foreach (var dice in Data.Dices)
                {
                    string prefabname = TableAgent.Instance.GetString("Suface", rows.ToString(), column.ToString());
                    column = column + 1;
                    MapManager.Instance.SpawnObj(dice.Value, prefabname);
                }
            }
        }
    }
}
